package com.intelligent.ispc.web.controller;

import com.google.common.collect.Lists;
import com.intelligent.ispc.common.dto.JsonPageResponseDto;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.common.web.Servlets;
import com.intelligent.ispc.core.dto.DUTAttributeDto;
import com.intelligent.ispc.core.dto.TestDataExDto;
import com.intelligent.ispc.core.dto.TestItemDto;
import com.intelligent.ispc.core.service.ReportService;
import com.intelligent.ispc.utils.SystemConfig;
import com.intelligent.ispc.web.utils.PageUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by dmrj on 2015/10/29.
 */

@Controller
@RequestMapping(value = "/history")
public class HistoryController {
    /*@Autowired
    private ReportService reportService;
*/
    @Autowired
    private ReportService reportService;

    private Integer draw = 0;

    @Autowired
    private SystemConfig systemConfig;

    @RequestMapping(value = "/historyView", method = RequestMethod.GET)
    public String historyReport(Model model) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted("analysis:report:view")) {
            return "error/noPermission";
        }
        return "analysis/serialNumberView";
    }

    @ResponseBody
    @RequestMapping(value = "/historySerialNumber", method = RequestMethod.POST)
    public JsonPageResponseDto historyShows(Model model, ServletRequest request) {
        try {
            Subject subject = SecurityUtils.getSubject();
            if (!subject.isPermitted("analysis:report:view")) {
                return JsonPageResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
            }
            JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(Lists.newArrayList());
            json.setRecordsTotal(0);
            json.setRecordsFiltered(0);
            json.setDraw(draw);
            return json;
        } catch (Exception e) {
            return JsonPageResponseDto.FAILURE().setMessage(e.getMessage());
        }
    }


    @ResponseBody
    @RequestMapping(value = "/serialNumber", method = RequestMethod.POST)
    public JsonPageResponseDto historyShow(Model model, ServletRequest request) {
        try {
            Subject subject = SecurityUtils.getSubject();
            if (!subject.isPermitted("analysis:report:view")) {
                return JsonPageResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
            }

            Map<String, Object> searchParam = Servlets.getParametersStartingWith(request, "search_");
            Pageable pageable = PageUtil.buildPageWithParameters(request, systemConfig.getPageSize(), "testData.startTime", Sort.Direction.ASC);
            Page<TestDataExDto> page = reportService.searchSerialNumber(searchParam, pageable);
            List<TestDataExDto> testDataDtoList = page.getContent();
            JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(testDataDtoList);
            json.setRecordsTotal(page.getTotalElements());
            json.setRecordsFiltered(page.getTotalElements());
            json.setDraw(draw);
            return json;
        } catch (Exception e) {
            return JsonPageResponseDto.FAILURE().setMessage(e.getMessage());
        }
    }

    @RequestMapping(value = "/gotoStation", method = RequestMethod.GET)
    public String jumpToStation(Model model, ServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted("analysis:report:view")) {
            return "error/noPermission";
        }
        return "analysis/stationAttribute";
    }

    @ResponseBody
    @RequestMapping(value = "/queryStation", method = RequestMethod.POST)
    public JsonPageResponseDto stationQuery(Model model, ServletRequest request) {
        try {
            Subject subject = SecurityUtils.getSubject();
            if (!(subject.isPermitted("analysis:report:view"))) {
                return JsonPageResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
            }

            Map<String, Object> searchParam = Servlets.getParametersStartingWith(request, "search_");
            Pageable pageable = PageUtil.buildPageWithParameters(request, systemConfig.getPageSize(), "testData.startTime", Sort.Direction.ASC);
            searchParam.put("page", pageable.toString());
            Page<TestDataExDto> testDataDtoPage = reportService.searchSerialNumber(searchParam, pageable);
            List<TestDataExDto> testDataDtoList = testDataDtoPage.getContent();
            JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(testDataDtoList);
            json.setRecordsTotal(testDataDtoPage.getTotalElements());
            json.setRecordsFiltered(testDataDtoPage.getTotalElements());
            json.setDraw(draw);
            return json;
        } catch (Exception e) {
            return JsonPageResponseDto.FAILURE().setMessage(e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/queryAttribute", method = RequestMethod.POST)
    public JsonPageResponseDto attrQuery(Model model, ServletRequest request) {
        try {
            Subject subject = SecurityUtils.getSubject();
            if (!(subject.isPermitted("analysis:report:view"))) {
                return JsonPageResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
            }

            Map<String, Object> searchParam = Servlets.getParametersStartingWith(request, "search_");
            List<DUTAttributeDto> dutAttributeDtoList = reportService.searchAttributes(searchParam);
            JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(dutAttributeDtoList);
            json.setRecordsTotal(dutAttributeDtoList.size());
            json.setRecordsFiltered(dutAttributeDtoList.size());
            json.setDraw(draw);
            return json;
        } catch (Exception e) {
            return JsonPageResponseDto.FAILURE().setMessage(e.getMessage());
        }
    }

    @RequestMapping(value = "/gotoItem", method = RequestMethod.GET)
    public String jumptoItem(Model model, ServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted("analysis:report:view")) {
            return "error/noPermission";
        }
        return "analysis/queryItems";
    }

    @ResponseBody
    @RequestMapping(value = "/queryItem", method = RequestMethod.POST)
    public JsonPageResponseDto itemQuery(Model model, ServletRequest request) {
        try {

            Subject subject = SecurityUtils.getSubject();
            if (!(subject.isPermitted("analysis:report:view"))) {
                return JsonPageResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
            }

            Map<String, Object> searchParam = Servlets.getParametersStartingWith(request, "search_");
            Page<TestItemDto> page = reportService.searchTestItem(searchParam, PageUtil.buildPageWithParameters(request, systemConfig.getPageSize()));
            List<TestItemDto> list = page.getContent();

            JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(list);
            json.setRecordsTotal(page.getTotalElements());
            json.setRecordsFiltered(page.getTotalElements());
            json.setDraw(0);
            return json;
        } catch (Exception e) {
            return JsonPageResponseDto.FAILURE().setMessage(e.getMessage());
        }
    }
}

